/*
Vocab.m - Copyright (c) 1992 NeXT Computer, Inc.

You may freely copy, distribute and reuse the code in this example.
NeXT Computer, Inc. disclaims any warranty of any kind, expressed or implied, 
as to its fitness for any particular use.
*/
 
#import <appkit/appkit.h>

#import "Vocab.h"
#import "MainDelegate.h"
#import	"FindPanel.h"

@implementation VocabularyControl

- setBrowser:aBrowser { browser= aBrowser; return self; }

- windowDidBecomeKey:sender
{
    [self setTarget:self];
    return self;
}

- setTarget:aTarget
{
    id window = [browser window];
    target = aTarget;
    cursor = [[[NXApp delegate] recordManager] cursorForAttributeNamed:"TheContent"];
    [browser setDoubleAction:@selector(browserDoubleClick:)];
    [window disableFlushWindow];
    [browser loadColumnZero];
    [window reenableFlushWindow];
    [window flushWindow];
    return self;
}

- (int) browser:sender getNumRowsInColumn:(int) column
{
    return (count = [[cursor btree] count]);
}

- browser:sender loadCell:cell atRow:(int) row inColumn:(int) column
{
    int		i;
    int		times;
    char	*key;
    unsigned	length;

    count = [[cursor btree] count];
    if (hintRow == 0) [cursor setFirst];
    if (row < hintRow)
    {
	times = hintRow - row;
	if (row < times)
	{
	    [cursor setFirst];
	    for (i = 0; i < row; ++i)
		[cursor setNext];
	} else
	    for(i = 0; i < times; ++i)
		[cursor setPrevious];
    } else
    if (row > hintRow)
    {
	times = row - hintRow;
	if (times > (count - row))
	{
	    times = count - row;
	    [cursor setLast];
	    for(i = 1; i < times; ++i)
		[cursor setPrevious];
	} else
	    for(i = 0; i < times; ++i)
		[cursor setNext];
    }
    
    if ([cursor getKey:(void **)&key andLength:&length])
    {
	[cell setStringValue:key];
	[cell setLoaded:YES];
    }
    
    [cell setLeaf:YES];
    hintRow = row;
    return self;
}

- browserClick:sender
{
    [findPanel ping:self];
    [findPanel orderFront:self];
    [findPanel takeStringValueFrom:[[sender matrixInColumn:0] selectedCell]];
    return self;
}

- browserDoubleClick:sender
{
    [findPanel ping:self];
    [findPanel orderFront:self];
    [findPanel takeStringValueFrom:[[sender matrixInColumn:0] selectedCell]];
    [findPanel performFind:self];
    return self;
}
@end
